package com.xiaohong.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaohong.order.pojo.OrderTbl;
import com.xiaohong.order.service.OrderTblService;
import entity.Result;
import entity.StatusCode;
import io.swagger.v3.oas.annotations.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "OrderTbl接口文档")
@RestController
@RequestMapping("/orderTbl")
@CrossOrigin
public class OrderTblController {

    @Autowired
    private OrderTblService orderTblService;

    /***
     * 分页条件搜索实现OrderTbl
     * @param orderTbl
     * @param page
     * @param size
     * @return
     */
    @Operation(summary = "条件分页查询OrderTbl",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@RequestBody(required = false) @Parameter(name = "OrderTbl对象",required = true) OrderTbl orderTbl, @PathVariable int page, @PathVariable  int size){
        //调用OrderTblService实现分页条件查询OrderTbl
        Page pageInfo = orderTblService.findPage(orderTbl, page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 分页搜索实现OrderTbl
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @Operation(summary = "分页查询OrderTbl",description = "所需权限：")
    @Parameters({
            @Parameter(in = ParameterIn.PATH, name = "page", description = "当前页", required = true),
            @Parameter(in = ParameterIn.PATH, name = "size", description = "每页显示条数", required = true)
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<Page> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用OrderTblService实现分页查询OrderTbl
        Page pageInfo = orderTblService.findPage(page, size);
        return new Result<Page>(true,StatusCode.OK,"查询成功",pageInfo);
    }

    /***
     * 多条件搜索OrderTbl数据
     * @param orderTbl
     * @return
     */
    @Operation(summary = "多条件查询OrderTbl",description = "所需权限：")
    @PostMapping(value = "/search" )
    public Result<List<OrderTbl>> findList(@RequestBody(required = false) @Parameter(name = "OrderTbl对象",required = true) OrderTbl orderTbl){
        //调用OrderTblService实现条件查询OrderTbl
        List<OrderTbl> list = orderTblService.findList(orderTbl);
        return new Result<List<OrderTbl>>(true,StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据Id删除orderTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID删除OrderTbl",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable Integer id){
        //调用OrderTblService实现根据ID删除
        orderTblService.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

    /***
     * 根据ID修改OrderTbl数据
     * @param orderTbl
     * @param id
     * @return
     */
    @Operation(summary = "根据ID修改OrderTbl",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @PutMapping(value = "/{id}" )
    public Result update(@RequestBody @Parameter(name = "OrderTbl对象",required = true) OrderTbl orderTbl,@PathVariable Integer id){
        // 补充id
        orderTbl.setId(id);
        //调用OrderTblService实现修改OrderTbl
        orderTblService.update(orderTbl);
        return new Result(true,StatusCode.OK,"修改成功");
    }

    /***
     * 新增OrderTbl数据
     * @param orderTbl
     * @return
     */
    @Operation(summary = "添加OrderTbl",description = "所需权限：")
    @PostMapping
    public Result add(@RequestBody  @Parameter(name = "OrderTbl对象",required = true) OrderTbl orderTbl){
        //调用OrderTblService实现添加OrderTbl
//        int a = 1/0;
        orderTblService.add(orderTbl);
        return new Result(true,StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询OrderTbl数据
     * @param id
     * @return
     */
    @Operation(summary = "根据ID查询OrderTbl",description = "所需权限：")
    @Parameter(in = ParameterIn.PATH, name = "id", description = "ID", required = true)
    @GetMapping("/{id}")
    public Result<OrderTbl> findById(@PathVariable Integer id){
        //调用OrderTblService实现根据ID查询OrderTbl
        OrderTbl orderTbl = orderTblService.findById(id);
        return new Result<OrderTbl>(true,StatusCode.OK,"查询成功",orderTbl);
    }

    /***
     * 查询OrderTbl全部数据
     * @return
     */
    @Operation(summary = "查询所有OrderTbl",description = "所需权限：",security = {@SecurityRequirement(name ="Authorization")})
    @GetMapping
    public Result<List<OrderTbl>> findAll(){
        //调用OrderTblService实现查询所有OrderTbl
        List<OrderTbl> list = orderTblService.findAll();
        return new Result<List<OrderTbl>>(true, StatusCode.OK,"查询成功",list) ;
    }
}
